package com.bwda.dsrs.systemservice.feign;

import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.systemservice.aspect.LogTypeEnum;
import com.bwda.dsrs.systemservice.aspect.Loggable;
import com.bwda.dsrs.systemservice.controller.BaseController;
import com.bwda.dsrs.systemservice.domain.Constants;
import com.bwda.dsrs.systemservice.domain.condition.emailsettting.EmailSetttingCondition;
import com.bwda.dsrs.systemservice.domain.condition.msgtemplate.MsgTemplateCondition;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.po.emailsettting.EmailSetttingPo;
import com.bwda.dsrs.systemservice.domain.po.msgtemplate.MsgTemplatePo;
import com.bwda.dsrs.systemservice.domain.po.role.RolePo;
import com.bwda.dsrs.systemservice.domain.vo.emailsettting.EmailSetttingVo;
import com.bwda.dsrs.systemservice.domain.vo.msgtemplate.MsgTemplateVo;
import com.bwda.dsrs.systemservice.service.EmailSetttingService;
import com.bwda.dsrs.systemservice.service.MsgTemplateService;
import com.bwda.dsrs.systemservice.service.RoleService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 所有给其他服务调用的统一写在这个里面，但是其他人没写。
 * @author: yangjun
 * @create: 2019-09-12 10:12
 */
@RestController
@RequestMapping("/s/api")

public class ApiController extends BaseController {

    protected final Logger logger = LoggerFactory.getLogger(ApiController.class);
    @Autowired
    private EmailSetttingService emailSetttingService;

    @Autowired
    private MsgTemplateService msgTemplateService;
    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "查询邮件服务器（一个）", notes = "查询邮件服务器（一个）", httpMethod = "GET")
    @Loggable(descp = "查询邮件服务器（一个）", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryEmailSetting", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody
    EmailSetttingVo queryEmailSetting() throws SystemServiceException {
        EmailSetttingCondition condition = new EmailSetttingCondition();
        condition.setDelFlag(Constants.NOT_DELETED);
        condition.setState(Constants.NOT_ENABLE);
        List<EmailSetttingPo> list = emailSetttingService.queryList(condition);
        if (null == list || list.isEmpty()) {
            throw new SystemServiceException("邮件服务器配置错误");
        }
        EmailSetttingPo po = list.get(0);
        EmailSetttingVo vo = CopyUtil.transfer(po, EmailSetttingVo.class);
        return vo;
    }


    @RequestMapping(value = "/getMsgTemplateByCode", method = {RequestMethod.GET, RequestMethod.POST})
    public MsgTemplateVo getMsgTemplateByCode(@RequestParam(value = "code") String code) throws SystemServiceException {
        MsgTemplateCondition condition = new MsgTemplateCondition();
        condition.setDelFlag(Constants.ENABLE);
        condition.setCode(code);
        condition.setType(Constants.QUIT_STATUS);
        List<MsgTemplatePo> list = msgTemplateService.queryList(condition);
        if (null == list || list.isEmpty()) {
            throw new SystemServiceException("短信配置错误");
        }
        MsgTemplatePo po = list.get(0);
        MsgTemplateVo vo = CopyUtil.transfer(po, MsgTemplateVo.class);
        return vo;
    }

    @RequestMapping(value = "/queryRoleCodeList", method = {RequestMethod.GET, RequestMethod.POST})
    public List<String> queryRoleCodeList(@RequestParam(value = "userCode") String userCode) throws SystemServiceException {
        List<RolePo> rolePoList = roleService.queryUserRoleList(userCode);
        List<String> roleList =
                rolePoList.stream().map(po -> po.getRoleCode()).collect(Collectors.toList());
        return roleList;
    }


}